package com.cn.wanxi.service;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.model.CompanyModel;

import java.io.*;

public class CompanyService {
    private String splitString = "\n";
    private String path = "E:\\wanxi\\step-three\\restaurant03-1029\\web\\note";
    private String fileName = "company.txt";
    /**
     * 一个完整的方法
     * 权限修饰符 特征修饰符 返回值类型 方法名 （参数） 异常
     * {
     * 执行体
     * return 返回值；
     * }
     */
    /**
     * 1.需要将数据存入记事本
     * 2.将数据从记事本里面读取出来
     * 3.将数据封装成companyModel
     *
     * @return
     */
    public CompanyModel getCompanyModel() {
        //创建一个新的实力类，用于装数据，这个用于返回到页面上的数据

        /**
         * 1.写
         * 2.读
         * 3.封装
         */
//        wirteNote();
        String result = readNote();

        return setCompanyModel(result);
    }

    private CompanyModel setCompanyModel(String result) {
        CompanyModel companyModel = new CompanyModel();
        String[] results = result.split(splitString);
        companyModel.setLogo(results[0]);
        companyModel.setPhone(results[1]);
        companyModel.setNet(results[2]);
        companyModel.setEmail(results[3]);
        companyModel.setLinePhone(results[4]);
        companyModel.setAddress(results[5]);
        companyModel.setVersion(results[6]);
        return companyModel;
    }

    private String readNote() {

        StringBuilder stringBuilder = new StringBuilder();
        File file = new File(path, fileName);
        if (!file.exists()) {
            return "nofile";
        }
        try {
//            InputStream inputStream = new FileInputStream(file);
//            int result;
//            while ((result = inputStream.read()) != -1) {
//                System.out.print((char) result);
//            }
            Reader reader = new FileReader(file);
            int result;
//            String aa = new String();
//            StringBuffer bb = new StringBuffer();

            while ((result = reader.read()) != -1) {
//                System.out.print((char) result);
                stringBuilder.append((char) result);
            }
//            System.out.println(stringBuilder);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    private void wirteNote() {


        File fileDirectory = new File(path);

        if (!fileDirectory.exists()) {
            //不存在，则需要创建文件
            fileDirectory.mkdirs();
        }
        //存在则可以进入下一步
        File file = new File(fileDirectory, fileName);
        if (!file.exists()) {
            //存在，则可以进入下一步开始写
            //不存在，则创建文件
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            OutputStream outputStream = new FileOutputStream(file);
            CompanyDao companyDao = new CompanyDao();
            outputStream.write(companyDao.logo.getBytes());
            outputStream.write(splitString.getBytes());
            outputStream.write(companyDao.phone.getBytes());
            outputStream.write(splitString.getBytes());
            outputStream.write(companyDao.net.getBytes());
            outputStream.write(splitString.getBytes());
            outputStream.write(companyDao.email.getBytes());
            outputStream.write(splitString.getBytes());
            outputStream.write(companyDao.linePhone.getBytes());
            outputStream.write(splitString.getBytes());
            outputStream.write(companyDao.address.getBytes());
            outputStream.write(splitString.getBytes());
            outputStream.write(companyDao.version.getBytes());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
/**
 * io最重要的几个部分，file   字节流 字符流
 * 1.写
 *  判断当前文件夹是否存在  不存在 需创建
 *  判断当前文件是否存在   不存在需创建
 *  字节流 InputStream   FileInputStream
 *  字符转换为byte数组
 * 2.同样判断当前读取的文件是否存在，不存在，则直接返回，否则继续读取
 *   1）通过字节流读取
 *    需要将读取返回结果强制转换为字节
 *    需要注意while循环的用法
 *   2）通过字符流读取
 *     注意：通过字符流读取也会出现乱码
 *   3）String，StringBuilder，StringBuffer的区别？
 *   4）抽象类和接口的区别？
 * 3.封装model
 *    需要用到字符串的方法 split
 *    数组的用法，
 */
}
